<!DOCTYPE html>
<html>
<head>
	<title>DaWu</title>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="/css/index.css">
	<script type="text/javascript" src="/js/jquery-1.7.2.min.js"></script>
	<script type="text/javascript" src="/js/base64.min.js"></script>
	<script type="text/javascript" src="/js/jquery-cookie.js"></script>
	<script language="JavaScript" type="text/javascript" src="/js/func.js"></script>
	<script type="text/javascript" src="/lang/b28n.js"></script>
	<script type="text/javascript">
	</script>
</head>
<body>
	<div class="header header_login">
	    <h1 id="product-log"></h1>
	</div>
	<div class="login_body">
		<div class="loginbox">
			<form class="loginform" type="POST" name="LoginForm" action=""> 
				<div id="massage_text"></div>
				<div class="username loginline">
					<i></i>
					<input	type="text" name="userName">
				</div>
				<div class="password loginline">
					<i></i>
					<input	type="password" name="password">
				</div>
				<div class="password loginline" id="pd1" style="display:none">
					<i></i>
					<input	type="password" name="pass1word">
				</div>

				<div class="submit" id="lg" onclick="preSubmit();"><span id="login"> 登录 </span></div>

			</form>
		</div>
	</div>
</body>
<script>
Butterlate.setTextDomain("dwdoor");	
var lang;
function initTranslation()
{
    $("#login").text( _("Login") );
}	
$(function()
{
	if($.cookie('language') === "null" || !$.cookie('language'))
	{
		if(lang=='en')
		{
			$.cookie('language','en');
		}else if(lang=='zhcn' || !lang )
		{
			$.cookie('language','zhcn');
		}		
	}
	initTranslation();
});

function preSubmit() 
{
	if(document.forms[0].userName.value=="")
	{
		$('#massage_text').text('请输入用户名。');
		document.forms[0].userName.focus();
		return false;
	}
	if(document.forms[0].password.value=="")
	{
		$('#massage_text').text('请输入密码。');
		document.forms[0].password.focus();
		return false;
	}
	if(document.forms[0].password.value.match(/^[0-9A-Za-z_.@*-]{1,32}$/)==null)
	{
		$('#massage_text').text("只能输入数字、字母和_.@-*字符。");
		return false;
	}
	var password=document.forms[0].password.value;
	password = MD5(password);
	document.forms[0].password.value = utf16to8(password); 
	var loginData = "{\"username\":\"" + document.forms[0].userName.value + "\",\"password\":\"" + 
		document.forms[0].password.value+"\",\"type\":\"login\"}";

	var authSend="{\"action\":\"dwau\",\"audata\":\""+base64encode(loginData)+"\"}";
//	alert(authSend);

	$.ajax
	({
		url: "/dwbrowser.php",
		type: "POST",
		timeout: 180000,
		data: authSend,
		dataType: "text",
//			async: true,
		error: function() 
		{
			$('#massage_text').text('网络错误。'); 
			return false;
		},
		complete: function() 
		{		  
		  if (status === 'timeout') 
		  {
			$('#massage_text').text('超时。');
			this.abort();
		  }
		},
		success: function(auReturn) 
		{
			var auRe;               
			auRe = jQuery.parseJSON(Base64.decode(auReturn));
//			alert(auRe);
			if(auRe.res === "success")
			{

				var date = new Date();
				date.setTime(date.getTime() + (60 * 60* 1000));
				
				if(auRe.authstat=="4")
				{
					$('#massage_text').text('还未授权。'); 
					return false;
				}
				if(auRe.authstat=="3")
				{
					$.cookie('logon',true,{expires: date, path:"/"});
					$.cookie('dwadmin',true,{expires: date, path:"/"});
					dwuser=document.forms[0].userName.value;
					$.cookie('dwusername',dwuser,{expires: date, path:"/"});	
//					window.location.href='/home.php';
					window.location.href='/main.php';
				}
				if(auRe.authstat=="2") 
				{
//					alert("2");
					$.cookie('dwlogon2',true,{expires: date});
					$.cookie('dwadmin2',document.forms[0].userName.value);	
					window.location.href='/home2.php';
				}
				if(auRe.authstat=="1") 
				{
					$.cookie('dwlogon1',true,{expires: date});
					$.cookie('dwadmin1',document.forms[0].userName.value);
					window.location.href='/home1.php';
				}
				if(auRe.authstat=="0")
				{
					$.cookie('dwclientlogon',true,{expires: date});
					$.cookie('dwclient',document.forms[0].userName.value);
					window.location.href='/index.php';
				}else
				{
					$('#massage_text').text('出错了。重启或联系管理员。'); 
					return false;
				}					

			}else if(auRe.res === "deny1")
			{
				$('#massage_text').text('用户名错，请重新输入。'); 
				return false;
			}else if(auRe.res === "deny2")
			{
				$('#massage_text').text('密码错，请重新输入。'); 
				return false;
			}else if(auRe.res === "deny3")
			{
				$('#massage_text').text('请输入用户名。'); 
				return false;
			}else if(auRe.res === "deny4")
			{
				$('#massage_text').text('请输入密码。'); 
				return false;
			}else if(auRe.res === "deny5")
			{
				$('#massage_text').text('出错了。重启或联系管理员。'); 
				return false;
			}else if(auRe.res === "deny6")
			{
				$('#massage_text').text('用户名或密码太长。'); 
				return false;
			}else
			{
				$('#massage_text').text('出错了。重启或联系管理员。'); 
				return false;
			}								
		}
	});
}

$(document).keydown(function(e)
{
	if(e.keyCode === 13)
	{
		preSubmit();
	}
});
    
</script>
</html>